#include <gtk/gtk.h>
#include <stdlib.h>

int i = 0;
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *label3;
GtkWidget *button;

void on_clicked(GtkWidget *widget, gpointer data) //定义信号回调函数
{
    char a[20];
    i++;
    gcvt((float)i, 3, a);
    gtk_label_set_text(GTK_LABEL(label2), a); //设置按钮的标签
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "信号与事件");
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
    table = gtk_table_new(2, 3, FALSE); //定义一个2行3列的表格，单元格大小会根据单元格中的元件大小自动调整
    label1 = gtk_label_new("单击按钮第");
    label2 = gtk_label_new("0");
    label3 = gtk_label_new("次");
    button = gtk_button_new_with_label("计数");
    gtk_container_add(GTK_CONTAINER(window), table); //将表格添加到窗口中
    gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(0), 0, 10);
    gtk_table_attach(GTK_TABLE(table), label2, 1, 2, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(0), 0, 10);
    gtk_table_attach(GTK_TABLE(table), label3, 2, 3, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(0), 0, 10);
    gtk_table_attach(GTK_TABLE(table), button, 2, 3, 1, 2, (GtkAttachOptions)(0), (GtkAttachOptions)(0), 0, 10);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_clicked), window);
    gtk_widget_show_all(window); //显示窗口
    gtk_main();
    return 0;
}